define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'unilive/room/index' + location.search,
                    add_url: 'unilive/room/add',
                    edit_url: '',
                    detail_url: 'unilive/room/detail',
                    copy_url: 'unilive/room/copy',
                    del_url: '',
                    multi_url: 'unilive/room/multi',
                    sync_url: 'unilive/room/sync',
                    addgoods_url: 'unilive/room/addgoods',
                    table: 'unilive_room',
                }
            });

            // 合并操作方法
            $.extend(Table.api.events.operate,
                {
                    'click .btn-detail': function (e, value, row, index) {
                        e.stopPropagation();
                        e.preventDefault();
                        var table = $(this).closest('table');
                        var options = table.bootstrapTable('getOptions');
                        var ids = row[options.pk];
                        row = $.extend({}, row ? row : {}, {ids: ids});
                        var url = options.extend.detail_url;
                        Fast.api.open(Table.api.replaceurl(url, row, table), __('Detail'), $(this).data() || {});
                    }
                },
                {
                    'click .btn-copy': function (e, value, row, index) {
                        e.stopPropagation();
                        e.preventDefault();
                        var table = $(this).closest('table');
                        var options = table.bootstrapTable('getOptions');
                        var ids = row[options.pk];
                        row = $.extend({}, row ? row : {}, {ids: ids});
                        var url = options.extend.copy_url;
                        Fast.api.open(Table.api.replaceurl(url, row, table), __('Copy'), $(this).data() || {});
                    }
                },
                {
                    'click .btn-addgoods': function (e, value, row, index) {
                        e.stopPropagation();
                        e.preventDefault();
                        var table = $(this).closest('table');
                        var options = table.bootstrapTable('getOptions');
                        var ids = row[options.pk];
                        row = $.extend({}, row ? row : {}, {ids: ids});
                        var url = options.extend.addgoods_url;
                        Fast.api.open(Table.api.replaceurl(url, row, table), __('Add goods'), $(this).data() || {});
                    }
                }
            );

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'roomid', title: __('Roomid')},
                        {field: 'name', title: __('Name')},
                        {
                            field: 'cover_img',
                            title: __('Cover_img'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {
                            field: 'share_img',
                            title: __('Share_img'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {
                            field: 'start_time',
                            title: __('Start_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'end_time',
                            title: __('End_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {field: 'anchor_name', title: __('Anchor_name')},
                        {field: 'anchor_wechat', title: __('Anchor_wechat')},
                        {
                            field: 'live_status',
                            title: __('Live_status'),
                            searchList: {
                                "101": __('Live_status 101'),
                                "102": __('Live_status 102'),
                                "103": __('Live_status 103'),
                                "104": __('Live_status 104'),
                                "105": __('Live_status 105'),
                                "106": __('Live_status 106'),
                                "107": __('Live_status 107')
                            },
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'type',
                            title: __('Type'),
                            searchList: {"1": __('Type 1'), "0": __('Type 0')},
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'screen_type',
                            title: __('Screen_type'),
                            searchList: {"1": __('Screen_type 1'), "0": __('Screen_type 0')},
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'close_like',
                            title: __('Close_like'),
                            searchList: {"1": __('Close_like 1'), "0": __('Close_like 0')},
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'close_goods',
                            title: __('Close_goods'),
                            searchList: {"1": __('Close_goods 1'), "0": __('Close_goods 0')},
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'close_comment',
                            title: __('Close_comment'),
                            searchList: {"1": __('Close_comment 1'), "0": __('Close_comment 0')},
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('Detail'),
                                    classname: 'btn btn-xs btn-info btn-detail',
                                    extend: 'data-toggle="tooltip"',
                                    icon: 'fa fa-eye'
                                },
                                {
                                    name: 'copy',
                                    text: __('Copy'),
                                    classname: 'btn btn-xs btn-info btn-copy',
                                    extend: 'data-toggle="tooltip"',
                                    icon: 'fa fa-copy'
                                },
                                {
                                    name: 'addgoods',
                                    text: __('Add goods'),
                                    classname: 'btn btn-xs btn-info btn-addgoods',
                                    extend: 'data-toggle="tooltip"',
                                    icon: 'fa fa-plus'
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 同步微信小程序后台直播
            let parenttable = table.closest('.bootstrap-table');
            let options = table.bootstrapTable('getOptions');
            let toolbar = $(options.toolbar, parenttable);
            $(toolbar).on('click', '.btn-sync', function () {
                var options = {url: $.fn.bootstrapTable.defaults.extend.sync_url};
                Fast.api.ajax(options, function (data, ret) {
                    table.bootstrapTable('refresh');
                }, function (data, ret) {
                    //
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        copy: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    table: 'unilive_room',
                }
            });
            var table = $("#table-product");
            // 初始化表格
            table.bootstrapTable({
                data: tableProduct,
                pk: 'name',
                sortName: 'name',
                //快捷搜索,这里可在控制器定义快捷搜索的字段
                search: false,
                //启用普通表单搜索
                commonSearch: false,
                //显示导出按钮
                showExport: false,
                showHeader: true,
                checkboxHeader: false,
                operate: false,
                showToggle: false,
                showColumns: false,
                columns: [
                    [
                        {field: 'name', title: __('Product name')},
                        {
                            field: 'cover_img',
                            title: __('Cover_img'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {field: 'url', title: __('Url')},
                        {field: 'price', title: __('Price')},
                        {field: 'price2', title: __('Price2')},
                        {
                            field: 'price_type',
                            title: __('Pricetype'),
                            searchList: {"1": __('Pricetype 1'), "2": __('Pricetype 2'), "3": __('Pricetype 3')},
                            formatter: Table.api.formatter.status
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        addgoods: function () {

            //初始化表格参数配置
            Table.api.init({
                extend: {
                    table: 'unilive_room',
                }
            });

            // 已存在的商品列表
            var table = $('#table-product');
            //初始化表格
            table.bootstrapTable({
                data: tableProduct,
                pk: 'name',
                sortName: 'name',
                //快捷搜索,这里可在控制器定义快捷搜索的字段
                search: false,
                //启用普通表单搜索
                commonSearch: false,
                //显示导出按钮
                showExport: false,
                showHeader: true,
                checkboxHeader: false,
                operate: false,
                showToggle: false,
                showColumns: false,
                columns: [
                    [
                        {field: 'name', title: __('Product name')},
                        {
                            field: 'cover_img',
                            title: __('Cover_img'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {field: 'url', title: __('Url')},
                        {field: 'price', title: __('Price')},
                        {field: 'price2', title: __('Price2')},
                        {
                            field: 'price_type',
                            title: __('Pricetype'),
                            searchList: {"1": __('Pricetype 1'), "2": __('Pricetype 2'), "3": __('Pricetype 3')},
                            formatter: Table.api.formatter.status
                        },
                    ]
                ]
            });

            // 添加新的商品
            Form.events.faselect = function (form) {
                //绑定fachoose选择附件事件
                if ($(".fachoose", form).size() > 0) {
                    $(".fachoose", form).on('click', function () {

                        parent.Fast.api.open("unilive/goods/select?&multiple=true", __('Choose'), {
                            callback: function (data) {

                                // 是否新增
                                let append = false;
                                if ("undefined" == typeof dataProduct) {
                                    dataProduct = [];
                                } else {
                                    append = true;
                                }

                                outer:
                                    for (let i in data.data) {
                                        //去掉重复
                                        for (let ii in dataProduct) {
                                            if (dataProduct[ii].id == data.data[i].id) {
                                                continue outer;
                                            }
                                        }

                                        //默认数量
                                        data.data[i].number = 1;
                                        if (append) {
                                            // 向表单增加一行
                                            $('#table_product').bootstrapTable('append', data.data[i]);
                                        }
                                        dataProduct.push(data.data[i]);
                                    }

                                if (append) {
                                    return;
                                }

                                var table2 = $('#table-product-add');
                                // 初始化表格
                                table2.bootstrapTable({
                                    data: dataProduct,
                                    pk: 'id',
                                    pagination: false,
                                    search: false,
                                    commonSearch: false,
                                    showRefresh: false,
                                    showToggle: false,
                                    showColumns: false,
                                    showExport: false, //是否可导出数据
                                    columns: [
                                        [
                                            {field: 'id', title: __('Id'),formatter: function(value, row, index) {
                                                    //console.log(dataProduct);
                                                    return value+
                                                        '<input name="row[goodsId]['+index+']" value="'+row.goodsId+'" type="hidden" />' +
                                                        '<input name="row[name]['+index+']" value="'+row.name+'" type="hidden" />' +
                                                        '<input name="row[cover_img]['+index+']" value="'+row.coverImgUrl+'" type="hidden" />' +
                                                        '<input name="row[url]['+index+']" value="'+row.url+'" type="hidden" />' +
                                                        '<input name="row[price]['+index+']" value="'+row.price+'" type="hidden" />' +
                                                        '<input name="row[price2]['+index+']" value="'+row.price2+'" type="hidden" />' +
                                                        '<input name="row[price_type]['+index+']" value="'+row.priceType+'" type="hidden" />';
                                                }},
                                            {field: 'goodsId', title: __('Goodsid')},
                                            {field: 'name', title: __('Product name')},
                                            {
                                                field: 'coverImgUrl',
                                                title: __('Coverimgurl'),
                                                operate: false,
                                                events: Table.api.events.image,
                                                formatter: Table.api.formatter.image
                                            },
                                            {field: 'url', title: __('Url')},
                                            {field: 'price', title: __('Price')},
                                            {field: 'price2', title: __('Price2')},
                                            {
                                                field: 'priceType',
                                                title: __('Pricetype'),
                                                searchList: {"1": __('Pricetype 1'), "2": __('Pricetype 2'), "3": __('Pricetype 3')},
                                                formatter: Table.api.formatter.status
                                            },
                                            {
                                                field: 'operate',
                                                title: __('Operate'),
                                                table: table,
                                                events: Table.api.events.operate,
                                                formatter: function (value, row, index) {
                                                    var html = '<a href="javascript:$(\'#table-product-add\').bootstrapTable(\'remove\',{field: \'id\', values: ['+row.id+']});dataProduct = dataProduct.filter(function(item){return item.id != '+row.id+';});" class="btn btn-xs btn-danger"  data-original-title="删除"><i class="fa fa-trash">删除</i></a>'
                                                    return html;
                                                }}
                                        ]
                                    ],

                                });
                            }
                        });
                        return false;
                    });
                }
            };

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
